#include "../base.h"

#define LOG_TAG "LinearLayout"

extern jobject getActivity();

static jclass getClass(JNIEnv* env,const char * className)
{
	return (*env)->FindClass(env,className);
}

static jclass getLinearLayoutClass(JNIEnv* env)
{
	return getClass(env,"android/widget/LinearLayout");
}

//创建一个线性布局
LinearLayout newLinearLayout()
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getLinearLayoutClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(Landroid/content/Context;)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,getActivity());
	LinearLayout view = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return view;
}

//删除一个线性布局
void deleteLinearLayout(LinearLayout thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteLinearLayout thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//设置线性布局方向
void LinearLayout_setOrientation(LinearLayout thiz,int orientation)
{
	if(thiz==NULL)
	{
		LOGE("LinearLayout_setOrientation thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getLinearLayoutClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"setOrientation","(I)V");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->CallVoidMethod(env,thiz,methoID,orientation);
}

//获取线性布局方向
int LinearLayout_getOrientation(LinearLayout thiz)
{
	if(thiz==NULL)
	{
		LOGE("LinearLayout_setOrientation thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getLinearLayoutClass(env);
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID(env,clazz,"getOrientation","()I");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->CallIntMethod(env,thiz,methoID);
}


//创建一个线性布局的参数
LLayoutParams newLLayoutParams(int width,int height)
{
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/LinearLayout$LayoutParams");
	static jmethodID methoID = NULL;
	if(methoID==NULL)
		methoID = (*env)->GetMethodID( env,clazz,"<init>","(II)V" );
	jobject obj = (*env)->NewObject(env,clazz,methoID,width,height);
	LLayoutParams params = (*env)->NewGlobalRef(env,obj);
	(*env)->DeleteLocalRef(env,obj);
	(*env)->DeleteLocalRef(env,clazz);
	return params;
}

//删除一个线性布局
void deleteLLayoutParams(LLayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("deleteLLayoutParams thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	(*env)->DeleteGlobalRef(env,thiz);
}

//设置线性布局权重
void LLayoutParams_setWeight(LLayoutParams thiz,float weight)
{
	if(thiz==NULL)
	{
		LOGE("LLayoutParams_setWeight thiz==NULL");
		return ;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/LinearLayout$LayoutParams");
	static jfieldID fieldId= NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"weight","F");
	(*env)->DeleteLocalRef(env,clazz);
	(*env)->SetFloatField(env,thiz,fieldId,weight);
}

//得到线性布局权重
float LLayoutParams_getWeight(LLayoutParams thiz)
{
	if(thiz==NULL)
	{
		LOGE("LLayoutParams_getWeight thiz==NULL");
		return 0;
	}
	JNIEnv* env = Android_JNI_GetEnv();
	jclass clazz = getClass(env,"android/widget/LinearLayout$LayoutParams");
	static jfieldID fieldId= NULL;
	if(fieldId==NULL)
		fieldId = (*env)->GetFieldID(env,clazz,"weight","F");
	(*env)->DeleteLocalRef(env,clazz);
	return (*env)->GetFloatField(env,thiz,fieldId);
}




